
import UIKit

class TF_LivingReportView: TF_TopRightOrCornerTopLeft ,UITextViewDelegate {
    //默认为NO; 是否显示举报
    @objc public var isShowReport:Bool = false
    @objc public let end = UIButton()
    @objc public let textView = TF_TextView()
    @objc public let msgViewTextLength = UILabel()
    @objc public var msg:NSString = ""

    let label = UILabel()
    let line = UILabel()
    let btnArray = NSMutableArray()
    var KeyboardHeight:CGFloat = 0
    /*
     监听键盘的即将显示
     */
    @objc func keyboardWillShow(noti:NSNotification)
    {
    
        self.isShowReport = true

           //不是聊天不用处理
        if (self.currentTextView(self.textView) == true) {
                
            }else{
                
                return;
                
            }
        let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
        
        let keyboardFrameEnd:NSValue = keyboardInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
        
        let frame:CGRect = keyboardFrameEnd.cgRectValue
        self.KeyboardHeight = frame.size.height;
        // 修改底部约束
         let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height - frame.size.height - self.frame.size.height
    
         let animationDuration:NSNumber = keyboardInfo.value(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! NSNumber
         //  执行动画
         let duration = animationDuration.floatValue
        UIView.animate(withDuration: TimeInterval(duration)) {
            self.frame = CGRect(x: 0, y: chatBarY, width:  self.frame.size.width, height: self.frame.size.height)
        }

        
    }
    /**
     *  监听键盘的即将隐藏
     */
    @objc func KeyboardWillHide(noti:NSNotification)
    {
        if self.isShowReport {
            self.isShowReport = false

           
            if (self.currentTextView(self.textView) == true) {
                
                let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
                
                let animationDuration:NSNumber = keyboardInfo.value(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! NSNumber
                //  执行动画
                let duration = animationDuration.floatValue
                
            
                let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height - self.frame.size.height;
                
                UIView.animate(withDuration: TimeInterval(duration)) {
                    self.frame = CGRect(x: 0, y: chatBarY, width:  self.frame.size.width, height: self.frame.size.height)
                }
            
        }
        }
  
}
    override init(frame: CGRect) {
        super.init(frame: frame)
        //键盘显示前
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(noti:)), name: UIResponder.keyboardWillShowNotification, object: nil)

        //键盘隐藏前
        NotificationCenter.default.addObserver(self, selector: #selector(self.KeyboardWillHide(noti:)), name: UIResponder.keyboardWillHideNotification, object: nil)

        self.label.textAlignment = .center
        self.label.numberOfLines = 0
        self.addSubview(self.label)
        
        let labelString = TalkfunSharedTools.getAttributedText("举报原因", fontSize: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 35/255.0 , green: 57/255.0, blue: 64/255.0, alpha: 1))

        self.label.attributedText = labelString
        self.label.textColor = UIColor(red: 35/255.0 , green: 57/255.0, blue: 64/255.0, alpha: 1)
         //线
 
         self.line.backgroundColor = UIColor(red: 238/255.0 , green: 238/255.0, blue: 239/255.0, alpha: 1)

        self.addSubview(self.line)
        
        //投诉内容
    
        self.textView.tf_placeholder = "其他详细原因"
        self.textView.layer.borderColor =  UIColor(red: 235/255.0 , green: 239/255.0, blue: 242/255.0, alpha: 1).cgColor
        self.textView.layer.borderWidth = 0.5
        self.textView.layer.cornerRadius = 4
        self.addSubview(self.textView)
        self.textView.delegate = self;
        self.textView.isHidden = true
    
        self.msgViewTextLength.numberOfLines = 0;
        self.msgViewTextLength.text = "0/25";
        self.msgViewTextLength.textAlignment = .right;
        self.textView.addSubview(self.msgViewTextLength)
        self.msgViewTextLength.font = UIFont.PingFang(size: 14)
        self.msgViewTextLength.textColor = UIColor(red: 210/255.0 , green: 210/255.0, blue: 210/255.0, alpha: 1)
     

        self.end.isUserInteractionEnabled = false
        self.end.backgroundColor = UIColor(red: 229/255.0 , green: 229/255.0, blue: 229/255.0, alpha: 1)
        self.end.layer.cornerRadius = 4
        self.end.setTitle("确定", for: .normal)
        self.end.titleLabel!.font = UIFont.PingFang(size: 18)
        self.end.setTitleColor(UIColor(red: 111/255.0, green: 119/255.0, blue: 122/255.0, alpha: 1), for: .normal)
       
        self.addSubview(self.end)
    
        for i in stride(from: 0, to: 5 ,by: 1) {

            let btn = UIButton()
            btn.tag = i;
            if (i == 0)  {
                btn.setTitle("侵权", for: .normal)
            }else if (i == 1)  {
                btn.setTitle("色"+"情", for: .normal)
            }else if (i == 2)  {
                btn.setTitle("反动", for: .normal)
            }else if (i == 3)  {
                btn.setTitle("违法", for: .normal)
            }else if (i == 4)  {
                btn.setTitle("其他举报原因", for: .normal)
            }
            self.addSubview(btn)
            btn.layer.cornerRadius = 2;
            btn.titleLabel!.font = UIFont.PingFang(size: 16)
            btn.setTitleColor(UIColor(red: 111/255.0, green: 119/255.0, blue: 122/255.0, alpha: 1), for: .normal)
         
            btn.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1)
            
            btn.addTarget(self, action: #selector(self.btnClick), for: UIControl.Event.touchUpInside)
            self.btnArray.add(btn)
        }

        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.label.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 50)
    
        self.line.frame = CGRect(x: 0, y: 49.5, width: self.frame.size.width, height: 0.5)
       
        
        self.end.frame = CGRect(x: 22.5, y: self.frame.size.height - 20 - 44, width: self.frame.size.width - 22.5 - 22.5, height: 44)
        
        
        let msgViewH:CGFloat = 90
        self.textView.frame = CGRect(x: 22.5, y: self.end.frame.origin.y - 20 - msgViewH, width: self.end.frame.size.width, height: 90)

        let attrsSize: CGSize = "25/25".size(withAttributes: [NSAttributedString.Key.font: self.msgViewTextLength.font])

        
        
        self.msgViewTextLength.frame = CGRect(x: self.textView.frame.size.width - attrsSize.width - 8.5, y: self.textView.frame.size.height - 20 - 3.5, width: attrsSize.width, height: 20)
        for i in stride(from: 0, to: self.btnArray.count ,by: 1) {
            
            let btnWidth:CGFloat = (self.frame.size.width - 22.5 - 22.5 - 15 - 15)/3
            let btn =  self.btnArray[i] as! UIButton
            if (i == 0) {
                btn.frame = CGRect(x: 22.5, y: 64.5, width: btnWidth, height: 35)
                
            }else if (i == 1){
                btn.frame = CGRect(x: 22.5 + btnWidth + 15 , y: 64.5, width: btnWidth, height: 35)
        
            }else if (i == 2){
                btn.frame = CGRect(x: 22.5 + (btnWidth+15)*2, y: 64.5, width: btnWidth, height: 35)
           
            }
            else if (i == 3){
                btn.frame = CGRect(x: 22.5, y: 114.5, width: btnWidth, height: 35)
    
            }else {
                
                let btnWidth2:CGFloat = (self.frame.size.width - 22.5 - 22.5 - 15 )  - btnWidth;
                
                btn.frame = CGRect(x: 22.5 + btnWidth + 15, y: 114.5, width: btnWidth2, height: 35)
            }
          
            
        }
        
        
    }
    @objc func btnClick(btn:UIButton) {
        if ( self.end.isUserInteractionEnabled == false) {
            self.end.backgroundColor = UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1)
           
            self.end.isUserInteractionEnabled = true
            self.end.setTitleColor(UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1), for: .normal)
            
        }
        
        for i in stride(from: 0, to: self.btnArray.count ,by: 1) {
            
            let btn:UIButton = self.btnArray[i] as! UIButton

            btn.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1)
            btn.layer.borderColor = UIColor.clear.cgColor
            btn.layer.borderWidth = 0;
            
        }
        
        btn.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        btn.layer.borderColor = UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1).cgColor
        btn.layer.borderWidth = 0.5
        
        if (btn.tag == self.btnArray.count - 1) {
            self.textView.isHidden = false
         
        }else{
            self.msg = btn.titleLabel!.text! as NSString;
            self.textView.isHidden = true

        }
      
    }
    
    func textViewDidChange(_ textView: UITextView) {
        let text:NSString = self.textView.text! as NSString
        
        if (text.length > 25) {
            let subString1:NSString = text.substring(to: 25) as NSString as NSString

            self.textView.text = subString1 as String
        }
        let text1:NSString = self.textView.text! as NSString
        let msg = NSString.init(format: "%i/25", text1.length)
        
        self.msgViewTextLength.text = msg as String
        
        self.msg = self.msgViewTextLength.text! as NSString
    }
    @objc public func destroySharedIntance()
    {
        NotificationCenter.default.removeObserver(self)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
